<?
class MDecide
{
	private $_db;
	public function __construct()
	{
		$this->_db = Db::getInstance();
	}
	
	public function selectInformation($gatherId)
	{
		$sql = "select `number`,`holdDate`,`attendNum` from gather,gather_date 
			where `gather`.`gatherId`=$gatherId and `gather`.`gatherId`=`gather_date`.`gatherId` order by attendNum desc,holdDate asc";
		$inforItem = $this->_db->fetch_all($sql);
		$sql = "select `attendName` from gather_user where `gatherId`=$gatherId and `state`=1 ";
		$allPeopleArray = $this->_db->fetch_all($sql);
		$allPeople = array();
		foreach($allPeopleArray as $key=>$value)
			array_push($allPeople,trim($value['attendName']));
		$finallItem = array();
		foreach($inforItem as $keyBase=>$valueBase)
		{
			$date = $valueBase['holdDate'];
			$sql = "select `attendName` from gather_user as x,gather_attend as y 
				where `x`.`gatherId`=$gatherId and `x`.`attendTel`=`y`.`attendTel` and `y`.`freeDate`='$date' and `x`.`state`=1 ";
			$dayAttendArray = $this->_db->fetch_all($sql);
			$dayAttend = array();
			foreach($dayAttendArray as $key=>$value)
				array_push($dayAttend,trim($value['attendName']));
			$dayNoAttend = array_diff($allPeople,$dayAttend);
			/*
			$dayAttendString = "";
			$dayNoAttendString = "";	
			foreach($dayAttend as $key=>$value)
				$dayAttendString .=$value.";";
			foreach($dayNoAttend as $key=>$value)
				$dayNoAttendString .=$value.";";
			*/
			//$valueBase['attendPeople'] = $dayAttendString;
			//$valueBase['noAttendPeople'] = $dayNoAttendString;
			$valueBase['attendPeople'] = $dayAttend;
			$valueBase['noAttendPeople'] = $dayNoAttend;
			array_push($finallItem,$valueBase);
		}
		return $finallItem;
	}
	
	public function refusePeople($gatherId)
	{
		$sql = "select `attendName` from `gather_user` where `gatherId`=$gatherId and `state`=2";
		$rePeople = $this->_db->fetch_all($sql);
		$result = "";
		foreach($rePeople as $key=>$value)
			$result.=$value['attendName'].";";
		$sql = "select count(*) from `gather_user` where `gatherId`=$gatherId and `state`=2";
		$count = $this->_db->fetch_all($sql);
		$result = array("name"=>$result,"count"=>$count[0]['count(*)']);
		return $result;
	}
	
	public function noAnswerPeople($gatherId)
	{
		$sql = "select `attendName` from `gather_user` where `gatherId`=$gatherId and `state`=0";
		$noAnswer = $this->_db->fetch_all($sql);
		$result = "";
		foreach($noAnswer as $key=>$value)
			$result.=$value['attendName'].";";
		$sql = "select count(*) from `gather_user` where `gatherId`=$gatherId and `state`=0";
		$count = $this->_db->fetch_all($sql);
		$result = array("name"=>$result,"count"=>$count[0]['count(*)']);
		return $result;
	}
	
	public function updateFinalDate($gatherId,$date)
	{
		$sql = "update `gather` set `finalDate`='$date',`state`=1 where `gatherId`=$gatherId";
		$this->_db->query($sql);
		$sql = "select count(*) from gather_user where `gatherId`=$gatherId and `state`=1";
		$num = $this->_db->fetch_all($sql);
		$num = $num[0]['count(*)'];
		$sql = "update `gather` set `finalAttendNum`=$num where `gatherId`=$gatherId";
		$this->_db->query($sql);
	}
	
	public function deleteParty($gatherId)
	{
		$sql = "delete from `gather` where `gatherId`='{$gatherId}'";
		$this->_db->query($sql);
		$sql = "delete from `gather_attend` where `gatherId`='{$gatherId}'";
		$this->_db->query($sql);
		$sql = "delete from `gather_date` where `gatherId`='{$gatherId}'";
		$this->_db->query($sql);
		$sql = "delete from `gather_user` where `gatherId`='{$gatherId}'";
		$this->_db->query($sql);
	}
	
	public function array_diff_my($array_1, $array_2) 
	{
		$array_2 = array_flip($array_2);
		foreach ($array_1 as $key => $item) 
		{
			if (isset($array_2[$item]))
			{
				unset($array_1[$key]);
			}
		}
		return $array_1;
	}
	
	
	/**
	 *用户名密码验证
	 *@param $userTel 用户手机号
	 *@param $userPwd 用户密码
	 *@return boolean  
	 */
	public function authenticateUser($userTel, $userPwd)
	{
		$commit = false;
		$sql = "select * from `account` where `tel`='{$userTel}' and `password`='{$userPwd}'";
		$result = $this->_db->num_rows($sql);
		return $result>0 ? true : false;	
	}
}
?>